<?php
namespace Braintwister\Controller;
use Common\Controller\BraintwisterController;

class IndexController extends BraintwisterController {

    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }

    /**
     * 获取原来的闯关数
     */
    public function getcheckpoint_final(){

        $openid = I('post.openId','','htmlspecialchars');
        $sort = I('post.currSort','1','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');

        $code = I('post.state','1','htmlspecialchars');

        if (!$openid){
            $this->returnData($arr=array(),'参数错误','401');
        }

        $where1 = array(
            'openId'=>$openid,
        );

        if ($code =='1'){
            $data['checkpoint_final'] = $sort;
            M('Wxusers')->where($where1)->save($data);
        }



        $checkpoint_final = M('Wxusers')->field('wxusers_id,checkpoint_final')->where($where1)->find();

        $nowweek = (int)date('W');
        $year = date('Y');
        $wxusers_id = $checkpoint_final['wxusers_id'];

        $whrecod['year'] = $year;
        $whrecod['challenge_week'] = $nowweek;
        $whrecod['wxusers_id'] = $wxusers_id;
        $whrecod['appid'] = $appid;


        $update = array(
            'checkpoint_final'=>$checkpoint_final['checkpoint_final'],
            'exittime'=>date('Y-m-d H:i:s'),
            'appid'=>$appid,
        );


        $isin = M('challenge_week')->field('challenge_week_id')->where($whrecod)->find();
        //判断当周是否存在挑战记录
        if ($isin){
            M('challenge_week')->where($whrecod)->save($update);
            if ($code=='1'){
                M('challenge_week')->where($whrecod)->setInc('passnum');
            }
        }else{
            $update['year'] = $year;
            $update['challenge_week'] = $nowweek;
            $update['wxusers_id'] = $wxusers_id;
            $update['checkpoint_final'] = $checkpoint_final['checkpoint_final'];
            $update['createtime'] = date('Y-m-d H:i:s');
            $update['passnum'] = '0';
            M('challenge_week')->data($update)->add();

        }

        $count = M('Quersion')->count();
        if ($checkpoint_final){
            $checkpoint_final['count'] = $count;
            $this->returnData($checkpoint_final,'成功','200');
        }else{
            $this->returnData($arr=array(),'失败','100');
        }

    }

    /**
     * 广告
     */
    public function advertising(){

        $appid = I('post.appid','','htmlspecialchars');

        $data['ad'] = M('newsong_ad')->where(array('isshow'=>'1'))->select();

        foreach ($data['ad'] as $key=>$val){

            $data['ad'][$key]['logo'] = $this->url.$data['ad'][$key]['logo'];

        }

        $data['moreplay'] = array(
            'appid'=>'wx6b1c7907c5733fc9',
            'url'=>'pages/index/index?appid=wx402ce3c8d9a9de9b&page=pages/index/index'
        );

        $this->returnData($data);

    }


    /**
     * 获取题库进行答题
     */
    public function getquersion(){


        $openid = I('post.openId','','htmlspecialchars');

        $sort = I('post.sort','1','int');

        if ($sort<1){
            $this->returnData('','参数错误',401);
        }

        $quesion = M('Quersion')->field('answer,img_src,confusingdata,sort')->where(array('sort'=>$sort))->find();

        $url = $this->getDomain();

        $quesion['img_src'] = $this->url.$quesion['img_src'];

        if ($quesion){
            //1代表支付隐藏
            $this->returnData($quesion,'0','200');
        }else{
            $this->returnData($arr=array(),'失败','100');
        }

    }

    /**
     * 好友排序和所有排序
     */
    public function getranking(){

        $wxuserid = I('post.wxuserid','','htmlspecialchars');

        $state = I('post.state','1','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');


        if ($state==""){
            $this->returnData('','参数错误','401');
        }

        //世界排行
        if ($state == '1'){


            $user = $this->getweekcache($appid);//读取缓存数据

            $this->returnData($user,'成功','200');
        }
        //好友排行
        if ($state == '0' && $wxuserid){

            $where['uid'] = $wxuserid;


            $coupling_uids = M('WxusersRelationship')->where($where)->getField('coupling_uid',true);

            if (empty($coupling_uids)){
                $coupling_uids = array();
            }

            $coupling_uids[] = $wxuserid;

            $uids = array();
            $uids = M('WxusersRelationship')->where(array('coupling_uid'=>$wxuserid))->getField('uid',true);
            if (empty($uids)){
                $uids = array();
            }

            $ids = array_unique(array_merge($coupling_uids,$uids));//合并去重

            $where1['wxusers_id'] = array('in',$ids);
            $user = "";
            if ($ids){
                $user = M('Wxusers')->field('nickName,avatarUrl,checkpoint_final')->where($where1)->limit(200)->order('checkpoint_final desc')->select();
            }

            $this->returnData($user,'成功','200');

        }


    }



    /**
     * 好友关联
     */
    public function friendsassociated(){

        $uid = I('post.uid','','int');
        $coupling_uid = I('post.coupling_uid','','int');
        $appid = I('post.appid','','htmlspecialchars');

        if ($uid==""||$coupling_uid==""){
            $this->returnData('','参数不能为空',401);
        }

        if ($uid==$coupling_uid){
            $this->returnData('','自己不用关联自己',100);
        }


        $where['uid']=$uid;
        $where['coupling_uid']=$coupling_uid;
        $where['appid'] = $appid;

        $where1['uid']=$coupling_uid;
        $where1['coupling_uid']=$uid;
        $where1['appid'] = $appid;

        $relevancy = M('WxusersRelationship')->field('wxusers_relationship_id')->where($where)->find();

        $relevancy1 = M('WxusersRelationship')->field('wxusers_relationship_id')->where($where1)->find();
        //判断是否已关联(有待优化)
        if ($relevancy||$relevancy1){
            $this->returnData('','已关联',200);
        }

        $data = array(
            'uid'=>$uid,
            'coupling_uid' =>$coupling_uid,
            'appid'=>$appid,
            'createtime' =>time(),
        );

        $insert = M('WxusersRelationship')->data($data)->add();

        if ($insert){
            $this->returnData('','关联成功',200);
        }else{
            $this->returnData('','关联失败',100);
        }

    }

    /**
     * 更多好玩
     */
    public function getappshow(){

        $app = M('appshow')->field('isshow',true)->where(array('isshow'=>'1'))->select();

//        foreach ($app as $key=>$value){
//            $app[$key]['logo'] = $this->url.$app[$key]['logo'] ;
//        }
        $this->returnData($app);

    }

    /**
     * 存放模板消息
     */
    public function send()
    {
        $appid = I('post.appid','','htmlspecialchars');
        $touser = I('post.touser','','htmlspecialchars');
        $page = I('post.page','','htmlspecialchars');
        $form_id = I('post.form_id','','htmlspecialchars');
        $form_type = I('post.form_type','','htmlspecialchars');
        $secret = I('post.secret','','htmlspecialchars');

        $template_id = I('post.template_id','0','htmlspecialchars');

        if ($appid==""||$touser==""||$page==""||$form_id==""){
            $this->returnData('','参数错误','401');
        }

        $date = date('Y-m-d');
        $insert = array(
            'appid'=>$appid,
            'touser'=>$touser,
            'template_id'=>$template_id,
            'page'=>$page,
            'form_id'=>$form_id,
            'form_type'=>$form_type?$form_type:1,
            'status'=>'-1',
            'add_time'=>time(),
            'secret'=>$secret,
            'createtime'=>$date,
        );

        M('wxopen_template_send')->data($insert)->add();
        $this->returnData('成功','200');
        $where['touser'] = $touser;
        //$where['template_id'] = $template_id;
        $where['createtime'] = $date;
        $where['status'] = '-1';


        $ishave = M('wxopen_template_send')->field('wxopen_template_send_id')->where($where)->find();

        if ($ishave){
            M('wxopen_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
        }else{
            M('wxopen_template_send')->data($insert)->add();
        }

        $this->returnData('成功','200');

    }



}